package com.dhc.pro.controller;

import com.dhc.pro.pojo.User;
import com.dhc.pro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController //  RestController注解会自动把集合转成JSON对象
@CrossOrigin(value = "http://127.0.0.1:8848",maxAge = 1800,allowedHeaders = "*",methods = RequestMethod.POST)
public class UserController {
    @Autowired
    private UserService userService;
    //登录
    @PostMapping("/login")

    public User login(User user, HttpServletRequest request){
        User searchUser=userService.login(user);
        if (searchUser!=null){

            HttpSession session=request.getSession();
            session.setAttribute("userInfo",searchUser);
        }

        return searchUser;

    }
    //添加用户
    @PostMapping("/admin/addUser")

    public int addUser(User user,HttpServletRequest request){
        int result = userService.addUser(user);
        System.out.println(result);
        HttpSession session=request.getSession();
        session.setAttribute("result",result);
        return result;
    }


}
